package com.dk.controller;

import com.dk.api.IqcServiceAPI;
import com.dk.api.UserServiceAPI;
import com.dk.api.WarehouseServiceAPI;
import com.dk.pojo.MdVendor;
import com.dk.pojo.QcIqc;
import com.dk.pojo.Warehouse;
import com.dk.pojo.WmItemRecpt;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.RecptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author LTZ
 * @create 2023/5/11
 */
@RestController
@RequestMapping("recpt")
@RefreshScope
@Api(tags = "采购入库接口文档")
public class RecptController {

    @Autowired
    private RecptService recptService;
    @Autowired
    private IqcServiceAPI iqcServiceAPI;
    @Autowired
    private UserServiceAPI userServiceAPI;
    @Autowired
    private WarehouseServiceAPI warehouseServiceAPI;

    @GetMapping("getRecptInfo")
    @ApiOperation("采购入库模糊查询和分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数"),
            @ApiImplicitParam(name = "recptCode", value = "入库单编号"),
            @ApiImplicitParam(name = "recptName", value = "入库单名称"),
            @ApiImplicitParam(name = "vendorName", value = "供应商名称"),
            @ApiImplicitParam(name = "poCode", value = "订单编号"),
            @ApiImplicitParam(name = "recptDate", value = "入库日期"),
    })
    public Result getRecptInfo(@RequestParam(defaultValue = "1")Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              String recptCode, String recptName, String vendorName,
                              String poCode, Date recptDate){
        PageResult<WmItemRecpt> result = recptService.getRecptInfo(pageNum,pageSize,recptCode,recptName,vendorName,poCode,recptDate);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE,result);
    }

    @PostMapping("addRecpt")
    @ApiOperation("采购入库增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recptCode",value = "入库单编号",required = true),
            @ApiImplicitParam(name = "recptName",value = "入库单名称",required = true),
            @ApiImplicitParam(name = "iqcId",value = "来料检验单ID"),
            @ApiImplicitParam(name = "iqcCode",value = "来料检验单编号",required = true),
            @ApiImplicitParam(name = "poCode",value = "采购订单编号",required = true),
            @ApiImplicitParam(name = "vendorId",value = "供应商ID"),
            @ApiImplicitParam(name = "vendorCode",value = "供应商编码",required = true),
            @ApiImplicitParam(name = "vendorName",value = "供应商名称"),
            @ApiImplicitParam(name = "vendorNick",value = "供应商简称"),
            @ApiImplicitParam(name = "warehouseId",value = "仓库ID"),
            @ApiImplicitParam(name = "warehouseCode",value = "仓库编码"),
            @ApiImplicitParam(name = "warehouseName" , value = "仓库名称",required = true),
            @ApiImplicitParam(name = "recptDate" , value = "入库日期" , required = true),
            @ApiImplicitParam(name = "remark" , value = "备注"),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true),
    })
    public Result addRecpt(WmItemRecpt recptRecpt,@RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        QcIqc qcIqc = iqcServiceAPI.selectByIqcCode(recptRecpt.getIqcCode());
        MdVendor mdVendor = userServiceAPI.selectByVendorCode(recptRecpt.getVendorCode());
        Warehouse warehouse = warehouseServiceAPI.selectByWarehouseName(recptRecpt.getWarehouseName());
        if(qcIqc==null || mdVendor==null || warehouse==null){
            return new Result(ResultConstant.IQC_ADD_NULL, ResultConstant.IQC_ADD_MESSAGE);
        }
        return recptService.addRecpt(recptRecpt,qcIqc,mdVendor,warehouse,token);
    }

    @PutMapping("updateRecpt")
    @ApiOperation("采购入库修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recptId",value = "入库单id",required = true),
            @ApiImplicitParam(name = "recptCode",value = "入库单编号"),
            @ApiImplicitParam(name = "recptName",value = "入库单名称"),
            @ApiImplicitParam(name = "iqcId",value = "来料检验单ID"),
            @ApiImplicitParam(name = "iqcCode",value = "来料检验单编号"),
            @ApiImplicitParam(name = "poCode",value = "采购订单编号"),
            @ApiImplicitParam(name = "vendorId",value = "供应商ID"),
            @ApiImplicitParam(name = "vendorCode",value = "供应商编码"),
            @ApiImplicitParam(name = "vendorName",value = "供应商名称"),
            @ApiImplicitParam(name = "vendorNick",value = "供应商简称"),
            @ApiImplicitParam(name = "warehouseId",value = "仓库ID"),
            @ApiImplicitParam(name = "warehouseCode",value = "仓库编码"),
            @ApiImplicitParam(name = "warehouseName" , value = "仓库名称"),
            @ApiImplicitParam(name = "recptDate" , value = "入库日期"),
            @ApiImplicitParam(name = "remark" , value = "备注"),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true),
    })
    public Result updateRecpt(WmItemRecpt recptRecpt,@RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        if(recptRecpt.getIqcCode()==null && recptRecpt.getVendorCode()==null && recptRecpt.getWarehouseName()==null){
            return recptService.updateRecpt(recptRecpt,token);
        }else if(recptRecpt.getIqcCode()!=null && recptRecpt.getVendorCode()==null && recptRecpt.getWarehouseName()==null){
            QcIqc qcIqc = iqcServiceAPI.selectByIqcCode(recptRecpt.getIqcCode());
            return recptService.updateRecpt(recptRecpt,qcIqc,token);
        }else if(recptRecpt.getIqcCode()==null && recptRecpt.getVendorCode()!=null && recptRecpt.getWarehouseName()==null){
            MdVendor mdVendor = userServiceAPI.selectByVendorCode(recptRecpt.getVendorCode());
            return recptService.updateRecpt(recptRecpt,mdVendor,token);
        }else if(recptRecpt.getIqcCode()==null && recptRecpt.getVendorCode()==null && recptRecpt.getWarehouseName()!=null){
            Warehouse warehouse = warehouseServiceAPI.selectByWarehouseName(recptRecpt.getWarehouseName());
            return recptService.updateRecpt(recptRecpt,warehouse,token);
        }else if(recptRecpt.getIqcCode()!=null && recptRecpt.getVendorCode()!=null && recptRecpt.getWarehouseName()==null){
            QcIqc qcIqc = iqcServiceAPI.selectByIqcCode(recptRecpt.getIqcCode());
            MdVendor mdVendor = userServiceAPI.selectByVendorCode(recptRecpt.getVendorCode());
            return recptService.updateRecpt(recptRecpt,qcIqc,mdVendor,token);
        }else if(recptRecpt.getIqcCode()==null && recptRecpt.getVendorCode()!=null && recptRecpt.getWarehouseName()!=null){
            MdVendor mdVendor = userServiceAPI.selectByVendorCode(recptRecpt.getVendorCode());
            Warehouse warehouse = warehouseServiceAPI.selectByWarehouseName(recptRecpt.getWarehouseName());
            return recptService.updateRecpt(recptRecpt,mdVendor,warehouse,token);
        }else if(recptRecpt.getIqcCode()!=null && recptRecpt.getVendorCode()==null && recptRecpt.getWarehouseName()!=null){
            QcIqc qcIqc = iqcServiceAPI.selectByIqcCode(recptRecpt.getIqcCode());
            Warehouse warehouse = warehouseServiceAPI.selectByWarehouseName(recptRecpt.getWarehouseName());
            return recptService.updateRecpt(recptRecpt,qcIqc,warehouse,token);
        }else {
            QcIqc qcIqc = iqcServiceAPI.selectByIqcCode(recptRecpt.getIqcCode());
            MdVendor mdVendor = userServiceAPI.selectByVendorCode(recptRecpt.getVendorCode());
            Warehouse warehouse = warehouseServiceAPI.selectByWarehouseName(recptRecpt.getWarehouseName());
            return recptService.updateRecpt(recptRecpt,qcIqc,mdVendor,warehouse,token);
        }
    }

    @DeleteMapping("deleteRecpt")
    @ApiOperation("入库删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recptId", value = "入库id",required = true)
    })
    public Result deleteRecpt(Long recptId){
        recptService.deleteRecpt(recptId);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
    }

    @PostMapping("updateStatus")
    @ApiOperation("执行入库接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recptId", value = "入库id",required = true)
    })
    public Result updateStatus(Long recptId){
        WmItemRecpt recpt = recptService.selectRecptByRecptId(recptId);
        QcIqc qcIqc = iqcServiceAPI.selectByIqcCode(recpt.getIqcCode());
        return recptService.updateStatus(recpt,qcIqc);
    }

}